package com.qian.apiserver.controller;

import com.qian.apiserver.model.*;
import com.qian.apiserver.service.IMatchRecordService;
import com.qian.apiserver.service.IUserService;
import com.yaowk.api.common.BaseException;
import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/game/match")
@Api(tags = "比赛记录")
public class MatchController {
    @Autowired
    private IMatchRecordService matchRecordService;
    @Autowired
    private IUserService userService;
    @GetMapping("/all")
    @ApiOperation("不填所有记录 用户或时间或投注或输赢查询")
    public Map find(PageReq pageReq, MatchRecordModel model){
        if(!StringUtils.isEmpty(model.getUserName())){
            Pattern pattern = Pattern.compile("[0-9]*");
            if(pattern.matcher(model.getUserName()).matches()){
                User user = new User();
                user.setUserCode(Long.parseLong(model.getUserName()));
                user = userService.findOne(user);
                model.setUserId(user.getId());
                return matchRecordService.find(pageReq,model);
            }else{
                User user = new User();
                List list = new ArrayList();
                user.setUsername(model.getUserName());
                PageResp<User> pageResp = userService.find(pageReq,user);
                for(User user1:pageResp.getList()){
                    model.setUserId(user1.getId());
                    list.addAll(matchRecordService.findName(pageReq,model));
                }
                Map map = new LinkedHashMap();
                map.put("totalPage",pageResp.getTotalPage());
                map.put("totalRow",pageResp.getTotalRow());
                map.put("pageSize",pageResp.getPageSize());
                map.put("pageNumber",pageResp.getPageNumber());
                map.put("list",list);
                return map;
            }
        }
        return matchRecordService.find(pageReq,model);

    }


}
